package com.example.cryptodemo.aes;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 加密工具类
 *
 * @author Yunkai Luo
 * @since 17:30 2021/11/3
 **/
public class BizMsgCryptUtil {
    /**
     * 通过一个字符串获得加密工具
     *
     * @param encryptionKey 通过“:”分割
     * @return 加密工具
     */
    public static BizMsgCrypt getBizMsgCrypt(String encryptionKey) throws AesException {
        List<String> key = Stream.of(encryptionKey.split(BizMsgCrypt.COLON)).collect(Collectors.toList());
        if (key.size() != 2) {
            throw new AesException(AesException.IllegalAesKey);
        }
        DeveloperInformation devlop = new DeveloperInformation(key.get(0), key.get(1));
        return new BizMsgCrypt(devlop);
    }

    public static BizMsgCrypt getBizMsgCrypt(String token, String encodingAesKey) throws AesException {
        DeveloperInformation devlop = new DeveloperInformation(token, encodingAesKey);
        return new BizMsgCrypt(devlop);
    }

    public static <T> Message<T> getClearMessage(BizMsgCrypt bizMsgCrypt, T data) throws AesException {
        return bizMsgCrypt.getClearMessage(data, String.valueOf(System.currentTimeMillis()),
                generateNonce()
        );
    }

    public static <T> Message<T> getCompatibleMessage(BizMsgCrypt bizMsgCrypt, T data) throws AesException {
        return bizMsgCrypt.getCompatibleMessage(data, String.valueOf(System.currentTimeMillis()),
                generateNonce()
        );
    }

    public static Message<String> getEncryptMessage(BizMsgCrypt bizMsgCrypt, String data) throws AesException {
        return bizMsgCrypt.getEncryptMessage(data, String.valueOf(System.currentTimeMillis()),
                generateNonce()
        );
    }

    public static String getDecryptMsgMessage(BizMsgCrypt bizMsgCrypt, Message<String> message) throws AesException {
        String str = bizMsgCrypt.decryptMsg(message);
        message.setData(str);
        return str;
    }

    public static String generateNonce() {
        return BizMsgCrypt.getRandomStr(6);
    }

    // 生成签名信息
    private static String getSignature(String nonce, String payload, String secret, String timestamp) {
        return DigestUtils.sha1Hex(nonce + ":" + payload + ":" + secret + ":" + timestamp);
    }

    /**
     * 获得随机token
     *
     * @return token
     */
    public static String generateToken() {
        return BizMsgCrypt.getRandomStr(10);
    }

    /**
     * 获得43位随机EncodingAESKey
     *
     * @return EncodingAESKey 用于加密
     */
    public static String generateEncodingAesKey() {
        return BizMsgCrypt.getRandomStr(43);
    }

    /**
     * 通过加密工具获得UI上的“密钥”
     *
     * @param bizMsgCrypt
     * @return
     */
    public static String getTheEncryptionKey(BizMsgCrypt bizMsgCrypt) {
        return bizMsgCrypt.getTheEncryptionKey();
    }

    /**
     * 验证url是否为真，以及检验配置是否正确
     *
     * @param bizMsgCrypt 加密工具
     * @param signature   签名
     * @param timeStamp   时间戳
     * @param nonce       随机字符串
     * @param echoStr     随机字符串，已加密，需要解密并返回
     * @return 解密后的echoStr
     * @throws AesException 异常
     */
    public static String verifyUrl(BizMsgCrypt bizMsgCrypt,
                                   String signature, String timeStamp, String nonce, String echoStr) throws AesException {
        return bizMsgCrypt.verifyUrl(signature, timeStamp, nonce, echoStr);
    }
}
